home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12450 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  67 lines

  1. Newsgroups: comp.lang.c
  2. Path: intrepid.cia.com!not-for-mail
  3. From: actuary@nando.net  (Bill McCarthy)
  4. Subject: Re: File Open Problem - Please provide clues
  5. Message-ID: <31f7cc$d436.37b@intrepid.cia.com>
  6. Date: Sun, 31 Mar 1996 20:04:54 GMT
  7. References: <4jkq8h$1it6@useneta1.news.prodigy.com>
  8. Reply-To: actuary@nando.net (Bill McCarthy)
  9. X-Newsreader: IBM NewsReader/2 v1.2
  10.  
  11. In <4jkq8h$1it6@useneta1.news.prodigy.com>,
  12. DPMU12A@prodigy.com (Steve Bailey) writes:
  13.  
  14. >Hi I'm having problems with opening files which are named   
  15. >after simple numbers:                                       
  16. >2                                                           
  17. >5, etc.                                                     
  18. >The reason for naming them this way is because I'm writing a
  19. >program in C that increments a number, and then attempts to 
  20. >open the file that's actually named by that number.         
  21. >I get mismatched type errors when using FOPEN:              
  22. > int num;                                                   
  23. > filepointer = FOPEN(num,"r");            or                           
  24. > filepointer = FOPEN("num","r");
  25.  
  26. Please post minimal complete code.  Otherwise, one wonders if
  27. "filepointer" was ever defined.  Was this code placed inside a
  28. function.  Was the stdio.h header included.
  29.  
  30. There is no FOPEN() function in C.  Why not just use fopen()?
  31.  
  32. The fopen() function takes two arguments, both of which are
  33. pointers to const char.  In fopen( num, "r" ) you are asking that
  34. an int be treated as a pointer -- what do think the compiler
  35. will do with that?  Your fopen( "num", "r" ) should attempt to
  36. open a file named num.
  37.  
  38. Here a very simple program that will create a file named 1 (or
  39. rewrite an existing file with that name):
  40.  
  41. #include <stdio.h>
  42.  
  43. #define    NUMSTRSIZE    10
  44.  
  45. int main( void )
  46. {
  47.     FILE    *fp;
  48.     int    num = 1;
  49.     char    fn[ NUMSTRSIZE ];
  50.  
  51.     sprintf( fn, "%d", num );
  52.  
  53.     if ( fp = fopen( fn, "w" ) )
  54.     {
  55.         /* write to file here with error checking */
  56.         fclose( fp );
  57.     }
  58.  
  59.     return 0;
  60. }
  61.  
  62. Bill McCarthy
  63. actuary@nando.net
  64. Wendell, NC  USA
  65.  
  66.  
  67.